package com.xgc.erp.order.purchase.ctrl;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Record;
import com.xgc.common.base.BaseController;
import com.xgc.common.config.Constan;
import com.xgc.common.model.OutcomeDetail;
import com.xgc.common.visit.Visitor;
import com.xgc.common.vo.Feedback;
import com.xgc.erp.order.purchase.service.OutcomeDetailService;

import java.util.Date;

//@ControllerBind(path = "/erp/order/outcomeDetail")
public class OutcomeDetailController extends BaseController {
    @Inject
    OutcomeDetailService detailService;

    public void index(){
        String orderId = get("orderId");
        set("orderId",orderId);
        render("index.html");
    }

    /**
     * 订单明细
     */
    public void list(){
        String orderId = get("orderId");
        set("orderId",orderId);         // 页面隐藏框值填充
        Record record = new Record();
        record.set("order_id",orderId);
        renderJson(detailService.queryForListEq(getParaToInt("pageNumber",1), getParaToInt("pageSize",10),record,"order by create_time desc"));
    }

    public void add(){
        Integer orderId = getInt("orderId");
        set("outcomeDetail",new OutcomeDetail().setOrderId(orderId));
        render("add.html");
    }

    public void save(){
        OutcomeDetail outcomeDetail=getBean(OutcomeDetail.class);
        outcomeDetail.setDetailId(detailService.getPrimaryKey(Constan.SEQ_OUTCOME_DETAIL));
        Visitor vs = getVisitor();
        outcomeDetail.setCreator(vs.getCode());
        outcomeDetail.setCreateTime(new Date());
        if(!detailService.save(outcomeDetail)){
            renderJson(Feedback.error("失败!"));
        }
        render("add.html");
    }

    public void edit(){
        String detailId = get("detailId");
        OutcomeDetail outcomeDetail = (OutcomeDetail) detailService.findById(detailId);
        setAttr("outcomeDetail",outcomeDetail);
        render("edit.html");
    }

    public void update(){
        OutcomeDetail outcomeDetail=getBean(OutcomeDetail.class);
        outcomeDetail.setModifier(getVisitor().getCode());
        outcomeDetail.setUpdateTime(new Date());
        if(!detailService.save(outcomeDetail)){
            renderJson(Feedback.error("失败!"));
        }
        render("edit.html");
    }
}
